package cn.stylefeng.guns.sys.modular.system.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.guns.sys.core.log.LogObjectHolder;
import cn.stylefeng.guns.sys.modular.system.entity.BtyArchives;
import cn.stylefeng.guns.sys.modular.system.entity.BtyWx;
import cn.stylefeng.guns.sys.modular.system.service.BtyWxService;
import cn.stylefeng.guns.sys.modular.system.service.DeptService;
import cn.stylefeng.guns.sys.modular.system.warpper.NoticeWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 钉钉微信关联controller
 */
@Controller
@RequestMapping("/btyWx")
public class BtyWxController extends BaseController {


    private String PREFIX = "/modular/system/btyWx/";

    @Resource
    private BtyWxService btyWxService;

    @Resource
    private DeptService deptService;

    /**
     * 跳转到钉钉微信关联页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "btyWx.html";
    }

    /**
     * 获取通知列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String name, String shopName, String phone, String post, Long deptId, String regionOne, String regionTwo,HttpServletRequest request) {
        //获取用户的帐号
        //String openid=request.getParameter("openid");
;        String account = LoginContextHolder.getContext().getUser().getAccount();
        Page<Map<String, Object>> list = this.btyWxService.list(name, shopName, account, phone, post, deptId, regionOne, regionTwo);
        Page<Map<String, Object>> wrap = new NoticeWrapper(list).wrap();
        return LayuiPageFactory.createPageInfo(wrap);
    }

    /**
     * 跳转到修改列表
     */
    @RequestMapping("/btyWx_update/{id}")
    public String noticeUpdate(@PathVariable Long id, Model model) {
        BtyWx btyWx = this.btyWxService.getById(id);
        btyWx.setDeptName(deptService.name(btyWx.getDeptId()));
        model.addAllAttributes(BeanUtil.beanToMap(btyWx));
        LogObjectHolder.me().set(btyWx);
        return PREFIX + "edit.html";
    }

    /**
     * 修改列表
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(BtyWx btyWx) {
        if (ToolUtil.isOneEmpty(btyWx, btyWx.getId())) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }

        this.btyWxService.updateById(btyWx);
        return SUCCESS_TIP;
    }

    /**
     * 跳转到添加页面
     */
    @RequestMapping(value = "/btyWx_add")
    public String btyTxl_addct() {
        return PREFIX + "add.html";
    }

    /**
     * 添加数据
     *
     * @param btyWx
     */
    @RequestMapping(value = "/add")
    public void add(BtyWx btyWx) {
        this.btyWxService.add(btyWx);
    }

    /**
     * 查询职务
     */
    @RequestMapping(value = "/getPost")
    @ResponseBody
    public List<BtyArchives> getPost() {
        return btyWxService.getPost();
    }

    /**
     * 查询一级区域
     */
    @RequestMapping(value = "/getRegionOne")
    @ResponseBody
    public List<BtyArchives> getRegionOne() {
        return btyWxService.getRegionOne();
    }

    /**
     * 查询二级区域
     */
    @RequestMapping(value = "/getRegionTwo")
    @ResponseBody
    public List<BtyArchives> getRegionTwo(String region) {
        return btyWxService.getRegionTwo(region);
    }

}
